package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RefreshScope
public class PaymentController {
    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @RequestMapping("/payment/lb/{id}")
    public int getPt(@PathVariable("id") int id){
        return id;
    }
    @RequestMapping("/payment/lb")
    public String getPort(){
        return serverPort;
    }
    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentOk(@PathVariable("id") String id){
        String result = paymentService.paymentInfo_OK(id);
        log.info("-------result: "+result);
        return result;
    }
    @HystrixCommand(fallbackMethod = "paymentInfoHanlder",
            commandProperties = {@HystrixProperty(name =  "execution.isolation.thread.timeoutInMilliseconds",value = "1000")})
    @GetMapping("/payment/hystrix/timeout/{id}")
    public String paymentTimeout(@PathVariable("id") String id){
        String result = paymentService.paymentInfo_Timeout(id);
        log.info("-------result: "+result);
        return result;
    }
    //自动调用fallback中的方法
    public String paymentInfoHanlder(String id){
        return "系统繁忙1:线程池   "+Thread.currentThread().getName() +" payment_timeOut, id:  "+id+"   fallback:  enter falback";
    }

    @Value("${mcdull.name}")
    private String name;

    @RequestMapping("/payment/getName")
    public String getName(){
        return name;
    }
}
